﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;

namespace TransportAbstractionLibrary.TCP
{
    public sealed class ConnectionAccepterEvent : ConnectionAccepter
    {
        object eventLock = new object();

        public delegate void ConnectionAcceptedEvent(object sender, ConnectionInboundEventArgs args);

        ConnectionAcceptedEvent connectionAcceptedEvent;
        public event ConnectionAcceptedEvent ConnectionInbound
        {
            add
            {
                lock (eventLock)
                {
                    connectionAcceptedEvent += value;
                }
            }
            remove
            {
                lock (eventLock)
                {
                    connectionAcceptedEvent -= value;
                }
            }
        }

        public new int Port
        {
            get
            {
                return base.Port;
            }
        }

        public new void StartListening()
        {
            base.StartListening();
        }

        public new void StopListening()
        {
            base.StopListening();
        }
 
        protected override void InboundConnection(Socket socket)
        {
            lock (eventLock)
            {
                if (connectionAcceptedEvent != null)
                {
                    connectionAcceptedEvent.Invoke(this, new ConnectionInboundEventArgs(socket));
                }
            }
        }

        public ConnectionAccepterEvent(int port) : base(port)
        {
        }
    }
}
